import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_baichuan/flutter_baichuan.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    FlutterBaichuan.initAlibc(appName: '1', version: '2').then((res) {
      print('百川SDK初始化: ${res.toJson()}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: [
            ListTile(
              title: Text('初始化'),
              onTap: () async {
                final res =
                    await FlutterBaichuan.initAlibc(appName: '1', version: '2');
                print(
                    '初始化: code: ${res.errorCode},message: ${res.errorMessage}');
              },
            ),
            ListTile(
              title: Text('loginTaobao'),
              onTap: () async {
                final user = await FlutterBaichuan.loginTaoBao();
                print(user.toJson());
              },
            ),
            ListTile(
              title: Text('getAccessToken'),
              onTap: () async {
                final res = await FlutterBaichuan.getAccessToken(
                    appKey: '31528286', appName: "魔方黑卡");
                print(res.toString());
              },
            ),
            ListTile(
              title: Text('isLogin'),
              onTap: () async {
                final state = await FlutterBaichuan.isLogin();
                print('isLogin: $state');
              },
            ),
            ListTile(
              title: Text('getUserInfo'),
              onTap: () async {},
            ),
            ListTile(
              title: Text('getUtdid'),
              onTap: () async {
                final str = await FlutterBaichuan.getUtdid();
                print('getUtdid: $str');
              },
            ),
            ListTile(
              title: Text('logout'),
              onTap: () async {},
            ),
            ListTile(
              title: Text('openByCode'),
              onTap: () async {
                FlutterBaichuan.openByCode(
                    id: '614385291547',
                    pid: 'mm_1460680056_2101550125_110951050292',
                    relationId: '2763056884');
              },
            ),
            ListTile(
              title: Text('openByUrl'),
              onTap: () async {
                final id = '589143467457';
                final pid = 'mm_1460680056_2101550125_110951050292110951050292';
                final url =
                    'http://s.click.taobao.com/t?e=m%3D2%26s%3DZKIZooEVBBNw4vFB6t2Z2ueEDrYVVa64Dne87AjQPk9yINtkUhsv0E4BYqB8luu9oQ6fPR8Xw%2FvQcUnGrL%2FqDD9Tg0Fb%2F425nHUkT%2BNOsTPDQQJULEX8UGiV4vv2IvuFUyNpxLfgKr0jWpzpm6nEC2awcT8O43GZ9r13xgemG7K4%2FApvI96NZkS8%2FhABRCcNu9tpCMBoU7epLiHMx4MPkXEqY%2Bakgpmw&scm=1007.19011.125585.0_13366&pvid=4cdee41f-5f06-4db1-96f7-117940557e71&app_pvid=59590_11.186.101.209_861_1629959177627&ptl=floorId:13366;originalFloorId:13366;pvid:4cdee41f-5f06-4db1-96f7-117940557e71;app_pvid:59590_11.186.101.209_861_1629959177627&union_lens=lensId%3AMAPI%401629959177%400bba65d1_0f73_17b812345dc_3191%4001';

                FlutterBaichuan.openByUrl(
                    url: url, id: id, pid: pid, relationId: '2763056884');
              },
            ),
          ],
        ),
      ),
    );
  }
}
